package com.sjgtw.cloud.web.floodCtrlRecord.controller;

import com.sjgtw.cloud.common.core.web.controller.BaseController;
import com.sjgtw.cloud.common.core.web.domain.AjaxResult;
import com.sjgtw.cloud.common.core.web.page.TableDataInfo;
import com.sjgtw.cloud.util.EasyExcelUtil;
import com.sjgtw.cloud.web.floodCtrlRecord.domain.FloodCtrlRecord;
import com.sjgtw.cloud.web.floodCtrlRecord.service.IFloodCtrlRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 防洪抢修记录Controller
 * 
 * @author lvyoufang
 * @date 2024-03-14
 */
@RestController
@RequestMapping("/floodCtrlRecord")
@Api(tags = "4.6、防洪抢修记录")
public class FloodCtrlRecordController extends BaseController
{
    @Autowired
    private IFloodCtrlRecordService floodCtrlRecordService;

    /**
     * 查询防洪抢修记录列表
     */
    @ApiOperation(value = "查询防洪抢修记录列表")
    @GetMapping("/list")
    public TableDataInfo list(FloodCtrlRecord floodCtrlRecord)
    {
        startPage();
        List<FloodCtrlRecord> list = floodCtrlRecordService.selectFloodCtrlRecordList(floodCtrlRecord);
        return getDataTable(list);
    }

    /**
     * 获取防洪抢修记录详细信息
     */
    @ApiOperation(value = "获取防洪抢修记录详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(floodCtrlRecordService.selectFloodCtrlRecordById(id));
    }

    /**
     * 新增防洪抢修记录
     */
    @ApiOperation(value = "新增防洪抢修记录")
    @PostMapping
    public AjaxResult add(@RequestBody FloodCtrlRecord floodCtrlRecord)
    {
        return toAjax(floodCtrlRecordService.insertFloodCtrlRecord(floodCtrlRecord));
    }

    /**
     * 修改防洪抢修记录
     */
    @ApiOperation(value = "修改防洪抢修记录")
    @PutMapping
    public AjaxResult edit(@RequestBody FloodCtrlRecord floodCtrlRecord)
    {
        return toAjax(floodCtrlRecordService.updateFloodCtrlRecord(floodCtrlRecord));
    }

    /**
     * 删除防洪抢修记录
     */
    @ApiOperation(value = "删除防洪抢修记录")
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(floodCtrlRecordService.deleteFloodCtrlRecordByIds(ids));
    }


    /**
     * 导出
     */
    @ApiOperation(value = "导出")
    @PostMapping("/export")
    public void export(HttpServletResponse response, FloodCtrlRecord floodCtrlRecord)
    {
        List<FloodCtrlRecord> list = floodCtrlRecordService.selectFloodCtrlRecordList(floodCtrlRecord);
        EasyExcelUtil<FloodCtrlRecord> util = new EasyExcelUtil<>(FloodCtrlRecord.class);
        util.exportExcel(response, list, "防洪抢修记录", "防洪抢修记录", "floodCtrlRecord.xlsx");
    }

    /**
     * 下载模版
     */
    @ApiOperation(value = "下载模版")
    @PostMapping("/downloadTemplate")
    public void downloadTemplate(HttpServletResponse response)
    {
        EasyExcelUtil<FloodCtrlRecord> util = new EasyExcelUtil<>(FloodCtrlRecord.class);
        util.exportExcel(response, new ArrayList<>(), "防洪抢修记录", "防洪抢修记录模版", "floodCtrlRecord.xlsx");
    }

    /**
     * 导入
     */
    @ApiOperation(value = "导入")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file)
    {
        EasyExcelUtil<FloodCtrlRecord> util = new EasyExcelUtil<>(FloodCtrlRecord.class);
        List<FloodCtrlRecord> entities = util.readExcel(file, 0, 2);
        if (entities == null || entities.size() == 0) {
            return error("没有读取到数据");
        }
        entities = entities.subList(1, entities.size());
      /*  List<String> errors = floodCtrlRecordService.check(entities);
        if (errors.size() > 0) {
            return AjaxResult.error("", errors);
        }*/
        floodCtrlRecordService.batchSave(entities);
        return success();
    }

}
